use chrono::prelude::*; // 用来处理日期
use mysql::prelude::*;
use mysql::*;
use polars::prelude::*;

pub fn demo2() {
    println!("demo2");
    let url = "mysql://root:hdjt2022DATA!@192.168.111.205:3306/salary_chain";
    let pool = Pool::new(url).unwrap(); // 获取连接池
    let mut conn = pool.get_conn().unwrap(); // 获取链接

    let sql = "select * from salary_chain.salary_user";
    let rows: Vec<Row> = conn.query(sql).unwrap(); // 先获取所有行数据

    // 提取各列数据到 Vec
    let ids: Vec<i32> = rows.iter().map(|row| row.get(0).unwrap()).collect();
    let usernames: Vec<String> = rows.iter().map(|row| row.get(3).unwrap()).collect();
    let passwords: Vec<String> = rows.iter().map(|row| row.get(4).unwrap()).collect();
    let created_ats: Vec<NaiveDateTime> = rows.iter()
        .map(|row| row.get::<NaiveDateTime, _>(10).unwrap())
        .collect();
    let roles: Vec<String> = rows.iter().map(|row| row.get(12).unwrap()).collect();

    // 创建 DataFrame
    let df = df!(
        "id" => ids,
        "username" => usernames,
        "password" => passwords,
        "created_at" => created_ats,
        "role" => roles,
    )
    .unwrap();

    println!("{}", df);
}
